package com.uusee.crawler.pageprocessor.video;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.uusee.crawler.framework.Processor;
import com.uusee.crawler.model.CrawlStatusCodes;
import com.uusee.crawler.model.CrawlURI;
import com.uusee.crawler.util.DomainUtils;
import com.uusee.crawler.util.PageProcessorUtils;
import com.uusee.shipshape.sp.model.Ugc;

public class PlayPageProcessor extends Processor {
	private static final String LI_REG = "<li class=\"mt6\">[\\s]*?([\\S].*?)<.*?>";
	private static final String A_REG = "<a.*?>[\\s]*?([\\S].*?)</a>";
	private static final String P_REG = "<\\s*?p.*?>(.*?)<\\s*?/p\\s*?>";
	private static Log log = LogFactory.getLog("PlayPageProcessor");
	Map<String,String> channelMap = new HashMap<String,String>();
	Map<String,String> categoryMap = new HashMap<String,String>();
	private Properties pageProcessRegex;

	private boolean canProcess(CrawlURI crawlURI) {
		int crawlStatus = crawlURI.getCrawlStatus();
		if (crawlStatus == CrawlStatusCodes.FETCH_SUCCESS) {
			return true;
		} else if (crawlStatus == CrawlStatusCodes.FETCH_SC_NOT_OK) {
			crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_INVALID);
			return false;
		} else {
			return false;
		}
	}

	public void innerProcess(CrawlURI crawlURI) {
		if (!canProcess(crawlURI)) {
			return;
		}
		
		Date crawlDate = new Date();
		String videoSite = crawlURI.getSourceSite();
		String crawlUrl = crawlURI.getCrawlUrl();
		String crawlResult = crawlURI.getCrawlResult();
		
		Ugc newVideo = (Ugc)crawlURI.getModel();
		if (newVideo==null) {
			newVideo = new Ugc();
			crawlURI.setModel(newVideo);
		}
		try {
			String videoText = crawlResult;
			Set<Object> keySet = pageProcessRegex.keySet();
			for (Object key:keySet) {
				String name = (String)key;
				if (pageProcessRegex.containsKey(name)) {
					String value = PageProcessorUtils.getValue(pageProcessRegex.getProperty(name), videoText);
					if (StringUtils.isNotEmpty(value)&&PropertyUtils.isWriteable(newVideo, name)) {
						if (PageProcessorUtils.isMatch(A_REG, value)) {
							value = PageProcessorUtils.getValues(A_REG, value);
						}
						if (PageProcessorUtils.isMatch(LI_REG, value)) {
							value = PageProcessorUtils.getValues(LI_REG, value);
						}
						if (PageProcessorUtils.isMatch(P_REG, value)) {
							value = value.replaceAll("<\\s*/p\\s*>\\s*?<\\s*p[^<]*?>", "::P_FLAG");
							value = value.replaceAll("<BR>", "::P_FLAG");
							value = value.replaceAll("<[^<]*?>", "");
							value = value.replaceAll("::P_FLAG", "<br/>");
						}else{
							value = value.replaceAll("<[^<]*?>", "");
						}
						value = StringUtils.trim(value);
						value = PageProcessorUtils.unicodeToChinese(value);
						if (name.equals("totalPageView")||name.equals("totalComment")||name.equals("totalFav")) {
							value = value.replaceAll("\\D", "");
							int iValue = 0;
							try {
								iValue = Integer.valueOf(value);
							}catch(Exception e){}
							if (iValue != 0) {
								PropertyUtils.setProperty(newVideo, name, iValue);
							}
						}else if (name.equals("length")) {
							Integer length = PageProcessorUtils.getLength(value);
							if (length != 0) {
								PropertyUtils.setProperty(newVideo, name, length);
							}
						} else {
							if (StringUtils.isNotEmpty(value)) {
								PropertyUtils.setProperty(newVideo, name, value);
							}
						}
						
					}
				}
			}
			String domain = DomainUtils.getDomain(crawlUrl);
			String title = newVideo.getTitle();
			String vid = newVideo.getVid();
			String evid = newVideo.getEvid();
			String uploadUserid = newVideo.getUploadUserid();
			
			// 处理播放地址
			String playUrl = newVideo.getPlayUrl();
			String playUrlRegex = pageProcessRegex.getProperty("playUrl");
			if (StringUtils.isNotEmpty(playUrlRegex)) {
				String result = PageProcessorUtils.replaceFlag(playUrlRegex,"::VID",vid);
				result = PageProcessorUtils.replaceFlag(result,"::EVID",evid);
				result = PageProcessorUtils.replaceFlag(result,"::UPLOAD_USER_ID",uploadUserid);
				if (!result.equals(playUrlRegex)) {
					playUrl = result;
				}
			}
			if (StringUtils.isNotEmpty(playUrl) && !playUrl.startsWith("http://")) {
				playUrl = "http://" + domain + playUrl;
			}
			
			// 标题和播放地址不能为空
			if (StringUtils.isEmpty(title) || StringUtils.isEmpty(playUrl)) {
				log.info(videoSite+"-"+vid+"-数据已经失效。");
				crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_INVALID);
				return;
			}
			
			newVideo.setPlayUrl(playUrl);
			
			String sourceSite = PageProcessorUtils.getValue("http://([^/]*?)/", playUrl);
			String videoDomain = DomainUtils.getDomain(playUrl);
			
			if (pageProcessRegex.containsKey("channelId")) {
				String channelId=PageProcessorUtils.getValue(pageProcessRegex.getProperty("channelId"), videoText);
				String channel="";
				if (channelMap.containsKey(channelId)) {
					channel = channelMap.get(channelId);
				}
				newVideo.setChannel(channel);
			}
			if (pageProcessRegex.containsKey("channelName")) {
				String channelName=PageProcessorUtils.getValue(pageProcessRegex.getProperty("channelName"), videoText);
				channelName = PageProcessorUtils.getValues(A_REG,channelName);
				newVideo.setChannel(channelName);
			}
			if (pageProcessRegex.containsKey("categoryIds")) {
				String categorieIds = PageProcessorUtils.getValue(pageProcessRegex.getProperty("categoryIds"), videoText);
				String[] categoryIdArray = categorieIds.split("\\|");
				StringBuffer categorysSb = new StringBuffer();
				String categorys = "";
				for (String categoryId:categoryIdArray) {
					if (categoryMap.containsKey(categoryId)) {
						String category = categoryMap.get(categoryId);
						categorysSb.append(category);
						categorysSb.append(",");
					}
				}
				if (categorysSb.length()>0) {
					categorys = categorysSb.substring(0, categorysSb.length()-1);
				}
				newVideo.setCategory(categorys);
			}

			String shareUrl = newVideo.getShareUrl();
			String shareUrlReg = pageProcessRegex.getProperty("shareUrl");
			if (StringUtils.isNotEmpty(shareUrlReg)) {
				String result = PageProcessorUtils.replaceFlag(shareUrlReg,"::VID",vid);
				result = PageProcessorUtils.replaceFlag(result,"::EVID",evid);
				result = PageProcessorUtils.replaceFlag(result,"::UPLOAD_USER_ID",uploadUserid);
				if (!result.equals(shareUrlReg)) {
					shareUrl = result;
				}
			}
			newVideo.setShareUrl(shareUrl);
			if (pageProcessRegex.containsKey("uploadUserblog")) {
				String uploadUserblog=PageProcessorUtils.getValue(pageProcessRegex.getProperty("uploadUserblog"), videoText);
				if (StringUtils.isNotEmpty(uploadUserblog) && !uploadUserblog.startsWith("http://")) {
					uploadUserblog = "http://" + sourceSite + uploadUserblog;
				}
				newVideo.setUploadUserblog(uploadUserblog);
			}
			
			newVideo.setDomain(videoDomain);
			newVideo.setVideoSite(videoSite);
			newVideo.setSourceSite(sourceSite);
			newVideo.setStatus("1");
			newVideo.setCrawlUser("uusee_crawler");
			newVideo.setCrawlDate(crawlDate);
			newVideo.setUpdateUser("uusee_crawler");
			newVideo.setUpdateDate(crawlDate);
			crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_SUCCESS);
			
		} catch (Exception e) {
			e.printStackTrace();
			log.error(videoSite+"-"+crawlUrl+"-处理时发生错误。",e);
			crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_EXCEPTION);
		}
	}
	
	

	public void setChannelMap(Map<String, String> channelMap) {
		this.channelMap = channelMap;
	}

	public void setCategoryMap(Map<String, String> categoryMap) {
		this.categoryMap = categoryMap;
	}

	public void setPageProcessRegex(Properties pageProcessRegex) {
		this.pageProcessRegex = pageProcessRegex;
	}
	
}
